package com.summerbird.mallchat.common.common.algorithm.ac;

import com.google.common.collect.Maps;
import lombok.Data;

import java.util.Map;

@Data
public class ACTrieNode {
    // 子节点
    private Map<Character, ACTrieNode> children = Maps.newHashMap();
    // 匹配过程中，如果模式串不匹配，则模式串会回退到failover继续匹配
    private ACTrieNode failover = null;
    // 树深度
    private int depth;
    // 是否是叶子节点
    private boolean isLeaf = false;

    public void addChildrenIfAbsent(char c){
        children.computeIfAbsent(c, (key) -> new ACTrieNode());
    }

    public ACTrieNode childOf(char c){
        return children.get(c);
    }

    public boolean hasChild(char c){
        return children.containsKey(c);
    }

    @Override
    public String toString() {
        return "ACTrieNode{" +
                "failover=" + failover +
                ", depth=" + depth +
                ", isLeaf=" + isLeaf +
                '}';
    }
}
